<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class User_model extends MY_Model {

	const TABLE = 'user';

	public function get_by_name($user_name) {
		$query = $this->db->get_where(self::TABLE, array('user_name' => $user_name));
		return $query->row_array();
	}

	public function add($user_name, $password, $role_id) {
		$data = array(
			'user_name' => $user_name,
			'password'  => $password,
			'role_id'   => $role_id
		);
		$this->db->insert(self::TABLE, $data);
		return $this->db->insert_id();
	}

	public function delete($user_id) {
		$this->db->where('user_id', $user_id);
		$this->db->delete(self::TABLE);
	}

	public function update($user_id, $user_name, $password, $role_id) {
		$data = array(
               'user_name' => $user_name,
               'password'  => $password,
               'role_id'   => $role_id
        );
		$this->db->where('user_id', $user_id);
		$this->db->update(self::TABLE, $data); 
	}

	public function get_user_list() {
		$query = $this->db->query('select * from user order by user_id desc');
		return $query->result_array();
	}

	public function get_by_id($user_id) {
		$query = $this->db->get_where(self::TABLE, array('user_id' => $user_id));
		return $query->row_array();
	}

}